<?php
namespace app\index\controller;

use think\Controller;
use think\Cookie;
use think\Db;
use app\index\model\BuserModel;
use think\Request;
use think\Session;

class Details extends Border
{
    //转发
    public function forwarding()
    {
        if (Session::get('sid')){
//            halt(11);
            $id = Session::get('sid');
            $username = Session::get('sname');
        }else{
//            halt(222);
            $username = Session::get('username');
            $id = Session::get('id');
        }
//        $id = 1;
        $type = input('type');
//        halt(input('type'));
        $attentId = input('id');
        //halt("测试"+$id);
//        $id = Session::get('id');

        //halt("测试"+$id);
        $info = Db::table(['wb_buser' => 'b', 'wb_buserinfo' => 'i'])
            ->field(['i.photo', 'b.username'])
            ->where('b.id=i.uid and b.id=' . $attentId)
            ->where('b.role=2')
            ->find();
        $articles = Db::table(['wb_buser' => 'b', 'wb_articles' => 'a'])
            ->field(['b.id', 'b.username', 'a.title', 'a.description', 'a.thumbnail', 'a.content', 'a.uid', 'a.status', 'b.role'])
            ->where('b.id = a.uid  and b.id=' . $attentId)
            ->where('b.role=2')
            ->select();
        $bad = Db::table(['wb_buser' => 'b', 'wb_bad' => 'd'])
            ->field(['b.id', 'b.username', 'd.title', 'd.path', 'd.uid', 'd.link'])
            ->where('b.id=d.uid and b.role=2 and b.id=' . $attentId)
            ->select();
        $attent = Db::table('wb_attent a')
            ->join('wb_buser b', 'a.attent_id=b.id')
            ->join('wb_buserinfo i', 'i.uid=b.id')
            ->field(['i.photo', 'b.username','a.attent_id','b.role'])
            ->where('a.user_id=' . $attentId)
            ->select();
        $fans = Db::table('wb_fans f')
            ->join('wb_buser b', 'f.fans_id=b.id')
            ->join('wb_buserinfo i', 'i.uid=b.id')
            ->field(['i.photo', 'b.username','f.fans_id','b.role'])
            ->where('f.user_id=' . $attentId)
            ->select();
        $message = Db::table('wb_message m')
            ->join('wb_buser b', 'm.uid=b.id', 'left')
            ->join('wb_mpicture p', 'p.mid=m.id ', 'left')
            ->join('wb_buserinfo i','b.id=i.uid','left')
            ->field(['m.content', 'm.time', 'm.collectnum', 'm.commentnum', 'm.transpondnum', 'm.agreenum', 'm.readnum', 'b.username', 'm.id', 'group_concat(p.path) path','i.photo','m.src_id'])
            ->where('b.id=' . $attentId)
            ->where('m.uid='. $attentId)
            ->order('m.id desc')
            ->group('m.id')
            ->select();

        foreach ($message as $k => $v) {

            if (!empty($v['src_id'])){
//                halt($v['src_id']);
                $res1 = Db::field('m.id id,u.username username,photo,content,time')
                    ->table(['wb_message' => 'm', 'wb_buser' => 'u','wb_buserinfo'=>'i'])
                    ->where('m.uid = u.id and  i.uid = u.id  and  m.id = '.$v['src_id'])
                    ->find();
//                halt($res1);
                $message[$k]['old'] = $res1;
                $path = Db::field('path')
                    ->table(['wb_message' => 'm','wb_mpicture'=>'mp'])
                    ->where('m.id = '.$v['src_id'].' and mp.mid = m.id')
                    ->find();
//                    halt($path);

                if (!empty($path)){
                    $message[$k]['old']['path'] = $path;
                }
//                    halt($path);
            }

        }
//            halt($message);
        // halt($attentId+";"+$id);


        if (!empty($id)) {
            $attentStatus = isAttent($id, $attentId);
        }else{
            $attentStatus=1;
        }


        if (empty($id)) {
            return view('index@business/index', [
                'attentStatus' => $attentStatus,
                'info' => $info,
                'articles' => $articles,
                'bad' => $bad,
                'attent' => $attent,
                'fans' => $fans,
                'message' => $message,
                'attentId' => $attentId,
                'id'=>$id

            ]);
        }

        $arr = Db::field('role')
            ->table(['wb_buser'])
            ->where('id = ' . $id)
            ->find();

        $role = implode("", $arr);

//        halt($id);
        $suer =Db::table('wb_buser')->field('role')->where('id',$id)->find();
//        halt($suer);
        if ($suer['role']==2){
            $p = Db::field('i.photo')
                ->table(['wb_buserinfo'=>'i','wb_buser'=>'b'])
                ->where('i.uid = '.$id)
                ->where('b.id=i.uid')
                ->where('b.role=2')
                ->find();
//            halt($p);
            if (!empty($p)) {
                $photo = implode("",$p);
            }
        }else{
            $p = Db::field('i.photo')
                ->table(['wb_buserinfo'=>'i','wb_buser'=>'b'])
                ->where('i.uid = '.$id)
                ->where('b.id=i.uid')
                ->where('b.role=1')
                ->find();
            if (!empty($p)) {
                $photo = implode("",$p);
            }
        }
        $user = Session::get('username');
        $id_p = Session::get('id');
        $pto = Db::table('wb_buserinfo')->field('photo')->where('uid',$id_p)->find();
        return view('index@activity/forwarding', [
            'attentStatus' => $attentStatus,
            'info' => $info,
            'articles' => $articles,
            'bad' => $bad,
            'attent' => $attent,
            'fans' => $fans,
            'message' => $message,
            'attentId' => $attentId,
            'photo'=>$photo,
            'username'=>$username,
            'user'=>$user,
            'id'=>$id,
            'type'=>$type,
            'pto'=>$pto

        ]);

    }

    //加载抽奖页面
    public function index()
    {
        return $this->fetch();
    }
    
    //投票
    public function vote()
    {
        $type = input('type');
        //判断是自己参与还是其他用户参与
        if (Session::get('sid')){
            $uid = Session::get('sid');
        }else{
            $uid = Session::get('id');
        }
        return view('activity/vote',[
            'type'=>$type,
            'uid'=>$uid
        ]);
    }

    //投票完后的
    public function votedo()
    {
//        halt(input());
//        halt(Cookie::get());
        $uid = input('uid');
        $type = input('type');

        $id = Session::get('id');
        $ar_insert =[
            'id'=>$id,
            'uid'=>$uid,
            'type'=>$type
        ];
        Db::table('wb_ar_relation')->insert($ar_insert);
        if (Session::get('sname')){
            $username =Session::get('sname');
        }else{
            $username =Session::get('username');

        }
//        halt(Session::get());
        $uid = Session::get('id');
        $p = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = ' . $uid)
            ->find();
        $photo = implode("", $p);
        $list = Db::field(['b.username', 'b.id','a.title','a.description','a.keywords','a.thumbnail','a.content','a.add_time','a.uid','a.status','a.type'])
            ->table(['wb_buser'=>'b','wb_articles'=>'a'])
            ->where(' b.id = a.uid and b.id= '.$uid)->select();
//        halt($list);
        return view('index@activity/server', [
            'list' => $list,
            'username'=>$username,
            'photo'=>$photo
        ]);
    }

}